package com.zhiyou.servlet;

import com.zhiyou.model.Lessee;
import com.zhiyou.service.ContractService;
import com.zhiyou.service.LesseeService;
import com.zhiyou.service.impl.ContractServiceImpl;
import com.zhiyou.service.impl.LesseeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class LesseeAddServlet extends HttpServlet {
    private LesseeService lS=new LesseeServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("添加执行");
        //创建租户对象
        Lessee lessee=null;
        String lname =req.getParameter("lname");
        String ltel = req.getParameter("ltel");
        String lsex = req.getParameter("lsex");
        String lnp = req.getParameter("lnp");
        String lidCard = req.getParameter("lidCard");
        String laddTime = req.getParameter("laddTime");
        if (lname!=""&&ltel!=""&&lsex!=""&&lnp!=""&&lidCard!=""&&laddTime!=""){
            lessee = new Lessee();//创建一个lessee对象来封装数据
            lessee.setLname(lname);
            lessee.setLtel(ltel);
            lessee.setLsex(Integer.parseInt(lsex));
            lessee.setLnp(lnp);
            lessee.setLidCard(lidCard);
            try {
                lessee.setLaddTime(new SimpleDateFormat("yy-MM-dd").parse(laddTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }



        if(lessee!=null){
            lS.addNew(lessee);
            //new HouseDao().addNew(haddress,hfloor,hroomNum,harea,hdir,hdeco,hair,hprice,hrentStatus,himage,haddTime,hupdateTime);
//            List<House> houses = hS.getHouse(1,3);
//            req.getSession().setAttribute("houses",houses);
            resp.sendRedirect(req.getContextPath()+"/user/lessee?pageNo="+req.getParameter("pageNo"));
        }else{
            req.setAttribute("err","注册失败,请将信息填写完整");
            resp.sendRedirect(req.getContextPath()+"/view/lessee/add.jsp");
        }

    }

}
